<?php
/**
 * 定义事件的使用方法
 */

namespace app\controllers;

use app\components\EventComponent;
use yii\base\Event;
use yii\web\Controller;

class EventController extends Controller
{
    /**
     * 组件使用on关联事件
     */
    public function actionIndex()
    {
        ini_set('output_buffering', true);
        $component = new EventComponent();
        $component->on(EventComponent::EVENT_SAVE_BEFORE, function(){
            echo "beforeSave one\n";
        });
        $component->on(EventComponent::EVENT_SAVE_BEFORE, function($event){
            //echo "beforeSave two\n";
            var_dump($event->name);
        });
        //$data,就是匿名函数中$event->data
        $component->on(EventComponent::EVENT_SAVE_BEFORE, function($event){
            echo "beforeSave step " . $event->data . "\n";
        }, 'three');
        //这里的$event->msg 就是触发事件时 传入的事件对象的属性
        $component->on(EventComponent::EVENT_SAVE_BEFORE, function($event){
            echo "beforeSave step " . $event->msg . "\n";
        });
        //$component->trigger(EventComponent::EVENT_SAVE_BEFORE);
        $component->save(); //触发事件调用在save()
    }

    /**
     * 自定义事件类
     * curl loalhost/event/custom-event
     */
    public function actionCustomEvent()
    {
        $component = new EventComponent();
        //这里的$event->msg 就是触发事件时 传入的事件类对象
        $component->on(EventComponent::EVENT_SAVE_BEFORE, function($event){
            echo "beforeSave step " . $event->msg . "\n";
        });
        //$event = new SaveEvent();
        //$event->msg = 'SaveEvent';
        $event = new Event(['sender' => $this]);
        $component->trigger(EventComponent::EVENT_SAVE_BEFORE, $event); //触发事件 是使用自定义事件类
    }

    /**
     * 自定义事件类对象
     * curl loalhost/event/custom-event-object
     */
    public function actionCustomEventObject()
    {
        $component = new EventComponent();
        //这里的$event->msg 就是触发事件时 传入的事件类对象
        $component->on(EventComponent::EVENT_SAVE_BEFORE, function($event){
            echo "beforeSave step " . get_class($event->sender) . "\n";
        });
        $event = new Event(['sender' => $this]);
        $component->trigger(EventComponent::EVENT_SAVE_BEFORE, $event); //触发事件 是使用自定义事件类
    }

    /**
     * 全局事件
     * curl loalhost/event/gloab-event
     */
    public function actionGloabEvent()
    {
        //绑定事件
        \Yii::$app->on('bar', function ($event) {
            echo get_class($event->sender);        // 显示当前触发事件的对象的类名称
        });
        //触发事件
        \Yii::$app->trigger('bar', new Event(['sender' => $this]));
    }

    /**
     * 类级别的事件触发
     * 每当 EventComponent 或其子类的实例触发  事件时， 这个事件处理器都会执行。
     * curl loalhost/event/class-event
     */
    public function actionClassEvent()
    {
        Event::on(EventComponent::class, EventComponent::EVENT_SAVE_BEFORE, function ($event) {
            var_dump($event->sender);  // 显示触发事件的组件
        });

        //触发方式一
        //Event::trigger(EventComponent::class, EventComponent::EVENT_SAVE_BEFORE);

        $component = new EventComponent();
        $component->save(); //触发事件调用在save()

    }
}